package com.chenss.dragonser.activity;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.CardView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chenss.dragonser.R;
import com.chenss.dragonser.app.AppManager;
import com.chenss.dragonser.base.BaseActivity;
import com.chenss.dragonser.listener.OnMyDialogTwoListener;
import com.chenss.dragonser.utils.ToastUtil;
import com.chenss.dragonser.view.XDialogTwo;

/**
 * PageName: 订单详情
 * Create by chenss at 2018/4/20 2:54
 */
public class OrderDetailActivity extends BaseActivity implements OnMyDialogTwoListener {
    LinearLayout ll_left;//左边按钮
    TextView tv_page_title;
    int status = 0;
    TextView tv_state;//状态
    TextView tv_bottom, tv_bottom_btn;//底部
    CardView card_wait;//付款详情
    CardView card_send;//配送详情
    CardView card_reason;//退货原因
    View view1;
    LinearLayout ll_order2;
    XDialogTwo dialogTwo;

    @Override
    public void initParms(Bundle parms) {
        status = getIntent().getExtras().getInt("status", 0);
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_order_detail;
    }

    @Override
    public void initView(View view) {
        dialogTwo = new XDialogTwo(this);
        ll_left = findView(R.id.ll_left);//左边按钮
        tv_page_title = findView(R.id.tv_page_title);
        ll_left.setVisibility(View.VISIBLE);
        ll_left.setOnClickListener(this);
        tv_page_title.setText("订单详情");


        view1 = findView(R.id.view1);
        ll_order2 = findView(R.id.ll_order2);
        tv_state = findView(R.id.tv_state);
        tv_bottom = findView(R.id.tv_bottom);
        tv_bottom_btn = findView(R.id.tv_bottom_btn);
        card_wait = findView(R.id.card_wait);
        card_reason = findView(R.id.card_reason);
        card_send = findView(R.id.card_send);
        tv_bottom_btn.setOnClickListener(this);
    }

    @Override
    public void doBusiness(Context mContext) {
//处理显示问题
        treasureShow();
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.ll_left:
                finish();
                break;
            case R.id.tv_bottom_btn:
                switch (status) {
                    case 1:
                        showSendGoodsDialog();

                        break;
                    case 3:
                        dialogTwo.showTwoDialog(this, "确认退款？", "确定", "取消", "提示", 1);
                        break;
                }
                break;
        }
    }

    public void treasureShow() {
        switch (status) {
            case 1:
                tv_state.setText("待发货");
                tv_bottom.setText("商品总价：");
                tv_bottom_btn.setText("确认发货");
                tv_bottom_btn.setBackgroundColor(ContextCompat.getColor(this, R.color.appColor));
                card_wait.setVisibility(View.GONE);
                card_send.setVisibility(View.GONE);
                card_reason.setVisibility(View.GONE);
                view1.setVisibility(View.VISIBLE);
                ll_order2.setVisibility(View.VISIBLE);
                break;
            case 2:
                tv_state.setText("已发货");
                tv_bottom.setText("商品总价：");
                tv_bottom_btn.setText("已发货");
                tv_bottom_btn.setBackgroundColor(ContextCompat.getColor(this, R.color.textLightGray));
                card_wait.setVisibility(View.VISIBLE);
                card_send.setVisibility(View.VISIBLE);
                card_reason.setVisibility(View.GONE);
                view1.setVisibility(View.VISIBLE);
                ll_order2.setVisibility(View.VISIBLE);
                break;
            case 3:
                tv_state.setText("待退款");
                tv_bottom.setText("退款金额：");
                tv_bottom_btn.setText("确认退款");
                tv_bottom_btn.setBackgroundColor(ContextCompat.getColor(this, R.color.appColor));
                card_wait.setVisibility(View.VISIBLE);
                card_send.setVisibility(View.VISIBLE);
                card_reason.setVisibility(View.VISIBLE);
                view1.setVisibility(View.GONE);
                ll_order2.setVisibility(View.GONE);
                break;
            case 4:
                tv_state.setText("已退款");
                tv_bottom.setText("退款金额：");
                tv_bottom_btn.setText("已退款");
                tv_bottom_btn.setBackgroundColor(ContextCompat.getColor(this, R.color.textLightGray));
                card_wait.setVisibility(View.VISIBLE);
                card_send.setVisibility(View.VISIBLE);
                card_reason.setVisibility(View.VISIBLE);
                view1.setVisibility(View.GONE);
                ll_order2.setVisibility(View.GONE);
                break;
        }


    }

    /**
     * 确认发货提示框
     */
    protected void showSendGoodsDialog() {
        final Dialog dialog = new Dialog(this, R.style.Dialog_image);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog_input_express);
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        final EditText edt = (EditText) dialog.findViewById(R.id.edt_price);
        final EditText edt_order = (EditText) dialog.findViewById(R.id.edt_order);
        TextView tv_title = (TextView) dialog.findViewById(R.id.tv_title);
        Button btn_qx = (Button) dialog.findViewById(R.id.btn_one);
        Button btn_qd = (Button) dialog.findViewById(R.id.btn_two);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        if (!dialog.isShowing()) {
            dialog.show();
        }

        dialog.getWindow().setAttributes(params);
        btn_qx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();

            }
        });
        btn_qd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (TextUtils.isEmpty(edt.getText().toString())) {
                    ToastUtil.showShort(OrderDetailActivity.this, "请输入运费");
                    return;
                }
                if (TextUtils.isEmpty(edt_order.getText().toString())) {
                    ToastUtil.showShort(OrderDetailActivity.this, "请输入运单号");
                    return;
                }
                ToastUtil.showShort(OrderDetailActivity.this, "发货成功");
                status = 2;
                treasureShow();
                dialog.dismiss();

            }
        });

    }

    @Override
    public void onSubmitClick(int tag) {
        switch (tag) {
            case 1:
                showTwoDialog();
                break;
        }
    }

    /**
     * 输入金额提示
     */
    protected void showTwoDialog() {
        final Dialog dialog = new Dialog(this, R.style.Dialog_image);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog_input_price);
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        final EditText edt = (EditText) dialog.findViewById(R.id.edt_price);
        TextView tv_title = (TextView) dialog.findViewById(R.id.tv_title);
        Button btn_qx = (Button) dialog.findViewById(R.id.btn_one);
        Button btn_qd = (Button) dialog.findViewById(R.id.btn_two);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        if (!dialog.isShowing()) {
            dialog.show();
        }

        dialog.getWindow().setAttributes(params);

        btn_qx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();

            }
        });
        btn_qd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (!TextUtils.isEmpty(edt.getText().toString())) {
                    ToastUtil.showShort(OrderDetailActivity.this, "退款成功");
                    status = 4;
                    treasureShow();
                    dialog.dismiss();
                } else {
                    ToastUtil.showShort(OrderDetailActivity.this, "请输入金额");
                }
            }
        });

    }

    @Override
    public void onCancleClick(int tag) {

    }

}
